package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class ReflectDemo8 {
    public static void main(String[] args) throws Exception {
        File dir = new File(
                ReflectDemo8.class.getResource(".").toURI()
        );
        System.out.println(dir.getAbsolutePath());

        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));

        for (File file : subs) {
            String fileName = file.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName("reflect." + className);
            Object o = cls.newInstance();
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                String methodName = method.getName();
                //名字含有s并且参数的个数为0
                if (methodName.toLowerCase().contains("s") &&
                        method.getParameterCount() == 0) {
                    System.out.println("自动调用方法:" + methodName + "()");
                    method.invoke(o);
                }
            }
        }
    }
}